package com.ruoyi.web.course;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.vo.TCourseVo;
import com.ruoyi.course.service.ITCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 课程Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Api(tags = "课程Controller")
@RestController
@RequestMapping("/course/course")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TCourseController extends BaseController {

    private final ITCourseService tCourseService;

    @ApiOperation("查询课程列表")
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo<TCourseVo> list(TCourseVo entity) {
        return tCourseService.queryList(entity);
    }

    @ApiOperation("查询课程所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TCourseVo entity) {
        return AjaxResult.success("查询成功", tCourseService.queryAll(entity));
    }

    @ApiOperation("导出课程列表")
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCourseVo entity) {
        List<TCourseVo> list = tCourseService.queryAll(entity);
        ExcelUtil<TCourseVo> util = new ExcelUtil<>(TCourseVo.class);
        util.exportExcel(response, list, "课程数据");
    }

    @ApiOperation("获取课程详细信息")
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tCourseService.queryById(id));
    }

    @ApiOperation("新增课程")
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TCourseVo entity) {
        return toAjax(tCourseService.save(entity));
    }

    @ApiOperation("修改课程")
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TCourseVo entity) {
        return toAjax(tCourseService.updateById(entity));
    }

    @ApiOperation("删除课程")
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tCourseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
